import { ScrollView, View, Text, Image, Button } from '@tarojs/components';
import React, { Component } from 'react';
import Taro, { getCurrentInstance } from '@tarojs/taro';
import { AtActionSheet, AtActionSheetItem } from 'taro-ui';
import 'taro-ui/dist/style/components/action-sheet.scss';
import { connect } from 'react-redux';
import './index.less';
import * as T from './types';
import actions from './actions';
import { store2Props } from './selectors';
import BlacklistModel from '@/pages/common/blacklist-model';
import Tab from './components/tab';
import Add from '@/assets/image/community/add.png';
import search from '@/assets/image/community/search.png';
import api from 'api';

actions().actions.loadReducer();
// @ts-ignore
@connect<Partial<T.IProps>, any>(store2Props, actions)
export default class Index extends Component<Partial<T.IProps>, any> {
  constructor(props) {
    super(props);
    this.state = {
      navBarHeight: 0,
      windowHeight: 0,
    };
  }

  componentDidMount() {
    this.getNavHeight();
    this.props.actions.init();
  }

  componentDidShow() {
    // this.props.actions.init();
    const title = Taro.getStorageSync("taskModel");
    if (title) {
      Taro.showToast({
        title,
        icon: 'none',
      });
      Taro.removeStorageSync("taskModel");
    }
  }

  componentWillUnmount() {
    this.props.actions.clean();
  }

  async onShareAppMessage() {
    //触发分享，任务完成
    let task = Taro.getStorageSync('task');
    if (task) {
      task = JSON.parse(task)
      let id = task.id;
      const result = await api.TaskCenterController.completedTask({ id })
      Taro.removeStorageSync("task");
      Taro.setStorageSync("taskModel", `获得${task.taskInfoVO.rewardType == 0 ? task.taskInfoVO.rewardPoints : ""}${["积分", "优惠券", "实物商品"][task.taskInfoVO.rewardType]}${task.taskInfoVO.rewardType == 2 ? "请到我的-我的奖励中填写收货地址" : ""}`)
    }

    let { user } = Taro.getStorageSync('b2b-wechat@login');
    let detail = Taro.getStorageSync('postDetail');
    let arr = [];
    if (detail) {
      let path = `/pages/package-B/community-detail/index?id=${detail.id}&del=false`;
      let title = `【${user.name}】为您推荐`;
      let imageUrl = `https://ruizhizone.oss-cn-nanjing.aliyuncs.com/assets/image/none/share.png`;
      if (detail.url) {
        arr = detail.url.split(';');
        imageUrl = arr[0];
      }
      //页面内分享按钮
      return {
        title,
        imageUrl,
        path,
      };
    }

    let wechatShareInfo = JSON.parse(Taro.getStorageSync('wechatShareInfo'));
    return {
      title: wechatShareInfo.title,
      imageUrl: wechatShareInfo.imgUrl[0].url,
    };
  }

  getNavHeight() {
    let menuButtonObject = Taro.getMenuButtonBoundingClientRect();
    var sysinfo = Taro.getSystemInfoSync();
    let windowHeight = sysinfo.windowHeight;
    let statusBarHeight = sysinfo.statusBarHeight;
    let menuBottonHeight = menuButtonObject.height;
    let menuBottonTop = menuButtonObject.top;
    let navBarHeight = statusBarHeight + menuBottonHeight + (menuBottonTop - statusBarHeight) * 2;
    this.setState({
      navBarHeight,
      windowHeight,
    });
  }

  async addClick() {
    let {
      actions: { action },
    } = this.props;
    let itemStr = Taro.getStorageSync('b2b-wechat@login');
    if (!itemStr) {
      Taro.navigateTo({
        url: '/pages/package-C/login/login/index',
      });
      return;
    }
    if (Taro.getStorageSync('blacklistModel')) {
      action.commonChange('main.blacklistModel', true);
      return;
    }
    Taro.navigateTo({
      url: '/pages/package-B/community-newPost/index',
    });
  }

  render() {
    let { navBarHeight } = this.state;
    let {
      main: { showMode, blacklistModel },
      actions: { action },
    } = this.props;
    return (
      <View className="packageCommunity">
        <View className="communitySearch" style={{ height: navBarHeight }}>
          <View
            className="searchBox"
            onClick={() => {
              if (Taro.getStorageSync('blacklistModel')) {
                action.commonChange('main.blacklistModel', true);
                return;
              }
              Taro.navigateTo({
                url: '/pages/package-B/postSearch/index',
              });
            }}
          >
            <Image className="communitySearch-img" mode="widthFix" src={search} />
            <View className="text">请输入关键字搜索</View>
          </View>
        </View>
        <Tab />
        {/* 新增 */}
        <Image
          className="communityAdd"
          src={Add}
          onClick={() => {
            this.addClick();
          }}
        />
        <AtActionSheet
          isOpened={showMode}
          cancelText="取消"
          onCancel={() => action.commonChange('main.showMode', false)}
          onClose={() => action.commonChange('main.showMode', false)}
        >
          {/* @ts-ignore */}
          <AtActionSheetItem
            onClick={() => {
              action.commonChange('main.showMode', false);
            }}
          >
            <Button openType="share" className="pageMyCollectionE-sharebtn">
              分享
            </Button>
          </AtActionSheetItem>
        </AtActionSheet>
        <BlacklistModel
          isOpened={blacklistModel}
          onConfirm={() => {
            action.commonChange('main.blacklistModel', false);
          }}
          onCancel={() => {
            action.commonChange('main.blacklistModel', false);
          }}
        />
      </View>
    );
  }
}
